/*
* ====================================================================
* Copyright (c) 2013-2014 Neudesic.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that redistributions of source
* retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* This source file includes software written by Neudesic, LLC (http://www.neudesic.com).
*
* THIS SOFTWARE IS PROVIDED BY Neudesic, LLC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  FURTHER, IN NO EVENT SHALL
* Neudesic, LLC (THE AUTHOR) OR ANY CONTRIBUTORS BE LIABLE FOR ANY DAMAGES
* WHATSOEVER THAT ARE RELATED TO THE SOFTWARE [INCLUDING BUT NOT LIMITED TO,
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* SUCH AS LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION, HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)] EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* ====================================================================
*/
#ifndef __WINRTUTILS_H__
#define __WINRTUTILS_H__

#include <windows.h>
#include <hstring.h>
#include <string>
#include <wrl/wrappers/corewrappers.h>

#include <windows.networking.sockets.h>
#include <wrl.h>
#include <windows.storage.streams.h>

using ABI::Windows::Networking::Sockets::SocketErrorStatus;

using Microsoft::WRL::Wrappers::HString;
//#ifdef __cplusplus
//extern "C" {
//#endif

std::wstring ConvertToWideString(LPCSTR inString);

#define ShowInfoMessage(FMT, ...) 		\
	ShowRuntimeMessage(0, L"*INFO: " FMT, ##__VA_ARGS__);
#define ShowInfoMessageWithFunction(FMT, ...) 		\
	ShowRuntimeMessage(0, L"***INFO: function: \"%s()\" \nfile: \"%s\" line: \"%u\" \nMSG: " FMT, \
	::ConvertToWideString(__FUNCTION__).c_str(), ::ConvertToWideString(__FILE__).c_str(), __LINE__, ##__VA_ARGS__);
#define ShowErrorMessage(FMT, ...) 		\
	ShowRuntimeMessage(0, L"***ERROR: function: \"%s()\" \nfile: \"%s\" line: \"%u\" \nMSG: " FMT, \
		::ConvertToWideString(__FUNCTION__).c_str(), ::ConvertToWideString(__FILE__).c_str(), __LINE__, ##__VA_ARGS__);
#define ShowErrorMessageWithResult(HR, FMT, ...) 		\
	ShowRuntimeMessage(HR, L"***ERROR: function: \"%s()\" \nfile: \"%s\" line: \"%u\" \nMSG: " FMT, \
		::ConvertToWideString(__FUNCTION__).c_str(), ::ConvertToWideString(__FILE__).c_str(), __LINE__, ##__VA_ARGS__);
#define ContinueIfSucceeded(HR, FMT, ...) 		\
	CallSuccessful(HR, L"***ERROR: function: \"%s()\" \nfile: \"%s\" line: \"%u\" \nMSG: " FMT, \
		::ConvertToWideString(__FUNCTION__).c_str(), ::ConvertToWideString(__FILE__).c_str(), __LINE__, ##__VA_ARGS__)
#define ShowWarningMessage(FMT, ...) 		\
	ShowRuntimeMessage(0, L"***Warning: function: \"%s()\" \nfile: \"%s\" line: \"%u\" \nMSG: " FMT, \
	::ConvertToWideString(__FUNCTION__).c_str(), ::ConvertToWideString(__FILE__).c_str(), __LINE__, ##__VA_ARGS__);

SocketErrorStatus GetSocketErrorStatus(HRESULT error);
BOOL CallSuccessful(HRESULT hr, LPCWSTR format, ...);
errno_t ConvertToWideString(LPCSTR inString, size_t sizeInWords, PWSTR outString);
errno_t ConvertToMultiByteString(LPCWSTR inString, size_t sizeInBytes, PSTR outString);
std::wstring ConvertToWideString(const HSTRING* inString);
std::string ConvertToMultiByteString(const std::wstring& inString);
HString ConvertToHString(const wchar_t* inString);
HString ConvertToHString(const char* inString);
std::string ConvertToMultiByteString(const HSTRING* inString);
void ShowRuntimeMessage(HRESULT hr, LPCWSTR format, ...);

//#ifdef __cplusplus
//}
//#endif

#endif